package com.mnxc.gxa.controller;



import com.mnxc.gxa.dto.QueryUserDTO;
import com.mnxc.gxa.dto.Result;
import com.mnxc.gxa.dto.UserDTO;
import com.mnxc.gxa.entity.User;
import com.mnxc.gxa.service.IUserService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mnxc
 * @since 2023-06-29
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {
    @Resource
    IUserService userService;

    @GetMapping("/getAll")
    @ApiOperation(value = "获取用户接口",notes = "获取所有用户",httpMethod = "GET",produces = "application/json;charset=utf-8")
    @ApiResponses({
            @ApiResponse(code=1,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=0,message="后台服务错误")
    })
    public Result getAll(){
        return userService.getAll();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return userService.getUserById(id);
    }
    /*
     * @description: 分页查询
     * @author: mnxc
     * @date: 2023/6/30 11:12
     * @param: [page, pageSize, condition]
     * @return: com.mnxc.gxa.dto.Result
     **/
    @ApiOperation(value = "分页查询接口",notes = "分页查询",httpMethod = "GET",produces = "application/json;charset=utf-8")
    @ApiResponses({
            @ApiResponse(code=1,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=0,message="后台服务错误")
    })
    @GetMapping("/getByPageAndCondition")
    public Result getByPageAndCondition( Integer page, Integer pageSize, QueryUserDTO query) {
        return userService.getByPageAndCondition(page, pageSize, query);
    }

    @GetMapping("/resetPassword/{ids}")
    public Result resetPasswordByIds(@PathVariable List<Long> ids){
        return userService.resetPasswordByIds(ids);
    }
    /*
     * @description: 保存用户
     * @author: mnxc
     * @date: 2023/6/30 10:38
     * @param: [user]
     * @return: com.mnxc.gxa.dto.Result
     **/

    @PostMapping
    @ApiOperation(value = "新增用户接口",notes = "新增用户",httpMethod = "POST",produces = "application/json;charset=utf-8")
    @ApiResponses({
            @ApiResponse(code=1,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=0,message="后台服务错误")
    })
    public Result saveUser(@RequestBody User user){
        return userService.saveUser(user);
    }

    @DeleteMapping
    @ApiOperation(value = "删除用户接口",notes = "通过用户ID删除用户",httpMethod = "DELETE",produces = "application/json;charset=utf-8")
    @ApiResponses({
            @ApiResponse(code=1,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=0,message="后台服务错误")
    })
    public Result deleteUser(User user){
        return userService.deleteUser(user);
    }

    @PutMapping
    @ApiOperation(value = "修改用户信息接口",notes = "修改用户的信息",httpMethod = "PUT",produces = "application/json;charset=utf-8")
    @ApiResponses({
            @ApiResponse(code=1,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=0,message="后台服务错误")
    })
    public Result updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }
    @PutMapping("/freeze")
    @ApiOperation(value = "冻结用户的接口",notes = "冻结用户",httpMethod = "PUT",produces = "application/json;charset=utf-8")
    @ApiResponses({
            @ApiResponse(code=1,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=0,message="后台服务错误")
    })
    public Result freezeUser(@RequestBody User user){
        return userService.freezeUser(user);
    }

    @PutMapping("/active")
    @ApiOperation(value = "激活用户接口",notes = "激活用户",httpMethod = "PUT",produces = "application/json;charset=utf-8")
    @ApiResponses({
            @ApiResponse(code=1,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=0,message="后台服务错误")
    })
    public Result activeUser(@RequestBody User user){
        return userService.activeUser(user);
    }


    @ApiOperation(value = "用户登录接口",notes = "验证用户的用户名和密码",httpMethod = "POST",response = Result.class,produces = "application/json;charset=utf-8")
    @ApiResponses({
            @ApiResponse(code=1,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=0,message="后台服务错误")
    })
    @PostMapping("/login")
    public Result login(@RequestBody User user, @ApiIgnore HttpSession session){
        return userService.login(user,session);
    }

    // @GetMapping("/getMenusById/{id}")
    public Result getMenusById(@PathVariable Long id){
        return userService.getMenusById(id);
    }

    /*
     * @description: 获取用户菜单
     * @author: mnxc
     * @date: 2023/7/1 9:43
     * @param: [session]
     * @return: com.mnxc.gxa.dto.Result
     **/
    @GetMapping("/getMenus")
    public Result getMenus(@ApiIgnore HttpSession session){
        // 1.获取用户id
        UserDTO userDTO = (UserDTO) session.getAttribute("user");
        if (userDTO == null) {
            throw new RuntimeException("身份校验错误！");
        }
        Long id = userDTO.getId();
        return getMenusById(id);
    }

}

